package com.leo.collisions;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.leo.TankApp;
import com.leo.common.GameType;
import com.leo.common.ItemType;
import com.leo.component.TankLevelComponent;
import com.leo.effects.HelmetEffect;
import com.leo.effects.ShipEffect;
import javafx.util.Duration;

import java.util.List;

/**
 * 玩家和道具的碰撞效果
 */
public class ItemPlayerHandler extends CollisionHandler {
    public ItemPlayerHandler() {
        super(GameType.ITEM, GameType.PLAYER);
    }

    @Override
    protected void onCollisionBegin(Entity a, Entity b) {
        // 获取道具类型
        ItemType itemType = a.getObject("itemType");
        FXGL.play("item.wav");
        a.removeFromWorld();

        switch (itemType) {
            case STAR -> b.getComponent(TankLevelComponent.class).upgrade();
            case GUN -> b.getComponent(TankLevelComponent.class).gradeFull();
            case TANK -> b.getComponent(HealthIntComponent.class).restore(1);
            case HEART -> b.getComponent(HealthIntComponent.class).restoreFully();
            case BOMB -> {
                List<Entity> entitiesByType = FXGL.getGameWorld().getEntitiesByType(GameType.ENEMY);
                for (Entity entity : entitiesByType) {
                    FXGL.spawn("bomb", entity.getCenter());
                    entity.removeFromWorld();
                    FXGL.inc("destroyedEnemyAmount", 1);
                }
            }
            case SHIP -> b.getComponent(EffectComponent.class).startEffect(new ShipEffect(Duration.INDEFINITE));
            case TIME -> FXGL.<TankApp>getAppCast().freezeEnemy();
            case HELMET -> b.getComponent(EffectComponent.class).startEffect(new HelmetEffect(Duration.seconds(12)));
            case SPADE -> FXGL.<TankApp>getAppCast().reinforce();
            default -> FXGL.<TankApp>getAppCast().reinforce();
        }
    }
}
